package com.ybl.store.common.convention.exception;

import com.ybl.store.common.convention.errorcode.BaseErrorCode;

import java.util.Optional;

/**
 * 抽象异常类
 *
 * @see ClientException
 * @see ServerException
 * @see RemoteException
 */
public abstract class AbstractException extends RuntimeException {

    /**
     * 异常码
     */
    private final String code;

    /**
     * 异常信息
     */
    private final String message;

    public AbstractException(String message, Throwable throwable, BaseErrorCode errorCode) {
        super(message, throwable);

        this.code = errorCode.code();
        this.message = Optional.ofNullable(errorCode.message()).orElse(message);
    }

    public String code() {
        return code;
    }

    public String message() {
        return message;
    }

    @Override
    public String toString() {
        return String.format("%s { code='%s', message='%s' }", getClass().getSimpleName(), code, message);
    }
}
